package org.aes.util;

/**
 * 生成AES密码
 * @author 修行者
 *
 */
public class GeneratePassword {

	public static void main(String[] args) {
		
		String pwd = getCAesKey();
		
		System.out.println("AES密码C语言版：" + pwd);
		
		System.out.println("AES密码Java版：" + getJavaAesKey(pwd));
		
	}
	
	/**
	 * 随机生成C语言使用的AES密码
	 * @return
	 */
	public static String getCAesKey(){
		String str = "";
		for(int i = 0; i < 16; i++){
			int b = (int) (Math.random() * 256);
			if(i < 15)
				str += b + ",";
			else
				str += b;
		}
		return str;
	}
	
	/**
	 * 解析C语音密码字符串为java版
	 * @param code
	 * @return
	 */
	public static String getJavaAesKey(String code){
		String codes[] = code.split(",");
		String str = "";
		for(int i = 0; i < 16; i++){
			int b = Integer.parseInt(codes[i]);
			if(b > 127){
				b = b - 256;
			}
			if(i < 15)
				str += b + ",";
			else
				str += b;
		}
		return str;
	}
	
	/**
	 * 解析java密码字符串为C语言版
	 * @param code
	 * @return
	 */
	public static byte[] getAesKey(String code){
		String codes[] = code.split(",");
		byte[] bit = new byte[codes.length];
		for(int i = 0; i < 16; i++){
			int b = Integer.parseInt(codes[i]);
			if(b > 127){
				b = b - 256;
			}
			bit[i] = (byte) b;
		}
		return bit;
	}
}
